package com.cryann.mqttserver.mqttProcess.mqttMsgHandler;

import com.cryann.mqttserver.exception.MqttException;
import com.google.common.collect.ImmutableSet;
import io.netty.handler.codec.mqtt.*;
import org.springframework.stereotype.Component;

import java.util.Set;

/**
 * @author cRyann
 * @date 2018/12/28 0028 15:31
 * @description: [MQTT-2.3.1-5] QoS 设置为 0 的 PUBLISH 报文不能包含报文标识符。
 */
@Component
public class MQTT_2$3$1_5 extends AbstractMqttHandler {

    @Override
    protected Set<MqttMessageType> handlerType() {
        return ImmutableSet.of(MqttMessageType.PUBLISH);
    }

    @Override
    protected boolean checkMsg(MqttMessage request) {
        MqttPublishMessage message = (MqttPublishMessage) request;
        MqttPublishVariableHeader variableHeader = (MqttPublishVariableHeader) request.variableHeader();
        if ( message.fixedHeader().qosLevel().equals(MqttQoS.AT_MOST_ONCE) && variableHeader.packetId() > 0 ) {
            throw new MqttException("[MQTT-2.3.1-5] QoS 设置为 0 的 PUBLISH 报文不能包含报文标识符.");
        }
        return true;
    }
}
